Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define _PyErr_ChainExceptions() FFI for CPython #2788

Merged
merged 1 commit into from
Dec 1, 2022
Merged

Define _PyErr_ChainExceptions() FFI for CPython #2788

merged 1 commit into from
Dec 1, 2022

Conversation

ijl
Copy link
Contributor

@ijl ijl commented Nov 29, 2022

This appears to be the only way to chain exceptions via the FFI. It was introduced prior to CPython 3.7. It has some description of usage in PEP 490. There was a discussion of whether it should be made stable in BPO 44938.

Copy link
Member

@messense messense left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bors bot added a commit that referenced this pull request Dec 1, 2022
2788: Define _PyErr_ChainExceptions() FFI for CPython r=messense a=ijl

This appears to be the only way to chain exceptions via the FFI. It was introduced prior to CPython 3.7. It has some description of usage in PEP 490. There was a discussion of whether it should be made stable in BPO 44938.

Co-authored-by: ijl <ijl@mailbox.org>
@messense
Copy link
Member

messense commented Dec 1, 2022

It's not presented in PyPy C-API, might need a #[cfg(not(PyPy)]: https://foss.heptapod.net/pypy/pypy/-/blob/branch/py3.9/pypy/module/cpyext/pyerrors.py

bors r-

@bors
Copy link
Contributor

bors bot commented Dec 1, 2022

Canceled.

@davidhewitt
Copy link
Member

davidhewitt commented Dec 1, 2022

As another comment, it looks like the implementation basically just either calls PyErr_Restore if no exception is set, or otherwise chains the exception as the context of the currently-set one with PyException_SetContext.

Some possible follow-ups (maybe we can track in new issues):

  • We could add context() and set_context() to match cause / set_cause which we already bind.
  • We could add a high-level implementation of this PyErr::chain() which could be used in the abi3 / PyPy cases.

This appears to be the only way to chain exceptions via the FFI. It
was introduced prior to CPython 3.7. It has some description of usage
in PEP 490. There was a discussion of whether it should be made stable
in BPO 44938.
@ijl
Copy link
Contributor Author

ijl commented Dec 1, 2022

Ok, I've added the cfg for PyPy.

@davidhewitt
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 1, 2022

Build succeeded:

@bors bors bot merged commit 84d68d4 into PyO3:main Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants